package com.pl.aplustesttool.util
import kotlin.math.max
import kotlin.math.min

/**
 * @创建者：小垚
 * @时间：2022/10/19  10:25
 * @描述：
 */
object GeometryUtil {

    /**
     * 是否相交
     */
    fun isCollsionWithRect(x1: Float, y1: Float, w1: Float, h1: Float, x2: Float, y2: Float, w2: Float, h2: Float): Boolean {
        return when {
            x1 >= x2 && x1 >= x2 + w2 -> false
            x1 <= x2 && x2 >= x1 + w1 -> false
            y1 >= y2 && y1 >= y2 + h2 -> false
            y1 <= y2 && y2 <= y1 + h1 -> false
            else -> true
        }
    }

    /**
     * 相交区域
     */
    fun intersectArea(rect1: PairBean, rect2: PairBean): Float {
        var rect = PairBean()
        rect.minX = max(rect1.minX, rect2.minX)
        rect.minY = max(rect1.minY, rect2.minY)
        rect.maxX = min(rect1.maxX, rect2.maxX)
        rect.maxY = min(rect1.maxY, rect2.maxY)
        return if (rect.minX < rect.maxX && rect.minY < rect.maxY) {
            (rect.maxX - rect.minX) * (rect.maxY - rect.minY)
        } else {
            0.0f
        }
    }
}


data class PairBean(
    var minX: Float = 0.0f,
    var minY: Float = 0.0f,
    var maxX: Float = 0.0f,
    var maxY: Float = 0.0f,
)